// pages/personal/personal.js
const addressUrl = 'https://restapi.amap.com/v3/geocode/regeo'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    score: 0,
    tabbar: {},
    nav: [
	// 	{
    //     name: '我代办的违章',
    //     url: '/pages/agency/personal/agency-list',
    //     open_type: 'navigate'
    //   },
      {
        name: '我协助的找车',
        url: '/pages/agency/quest/order_list?fromway=personal',
        open_type: 'navigate'
      },
        {
            name: '我代办的临牌',
            url: '/pages/linpai/help-detail',
            open_type: 'navigate'
        },
      {
        name: '分销',
        url: '/pages/common/distribution',
        open_type: 'navigate'
      },
      {
        name: '账单',
        url: '/pages/common/bill',
        open_type: 'navigate'
      },
      {
        name: '切换到用户端',
        url: '/pages/car-owner/index',
        open_type: 'redirectTo'
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // app.editTabbar()
    this.setData({
      user: wx.getStorageSync('user')
    })
    this.loadData()
    this.getLocation().then(this.loadAddress)
  },
  onShow() {
    this.loadData()
  },
  loadData() {
    wx.showLoading({
      title: '加载中...',
    })
    app.http.post('rest/member/queryById.do').then(res => {
      this.setData({
        balance: res.obj.money ? res.obj.money : 0,
        score: res.obj.score ? res.obj.score : 0
      })
      wx.hideLoading()
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 获取经纬度
   */
  getLocation() {
    return new Promise((resolve, reject) => {
      app.toPromise(wx.getStorage, {
        key: "location"
      }).then(res => {
        //经纬度已存在
        return Promise.resolve({
          longitude: res.data.lng,
          latitude: res.data.lat,
        })
      }).catch(err => {
        return app.toPromise(wx.getLocation, {
          type: 'gcj02'
        })
      }).then(res => {
        const location = {
          lng: res.longitude,
          lat: res.latitude,
        }
        wx.setStorage({
          key: 'location',
          data: location,
        })
        this.setData({
          location
        })
        resolve()
      }).catch(err => {
        this.setData({ //没有授权
          addressStatus: 1
        })
        reject()
      })
    })
  },
  /**
   * 点击按钮打开设置授权
   */
  handGetLocation() {
    return new Promise((resolve, reject) => {
      app.toPromise(wx.getSetting).then(res => {
        if (res.authSetting['scope.userLocation']) { //已经授权
          this.setData({
            addressStatus: 2
          })
          resolve(this.getLocation())
        } else { //没有授权
          this.setData({
            addressStatus: 1
          })
        }
      }).catch(err => {
        this.setData({
          addressStatus: 1
        })
        reject()
      })
    })

  },

  /**
   * 获取地址
   */
  loadAddress() {
    return app.toPromise(wx.request, {
      url: addressUrl,
      data: {
        key: app.globalData.key,
        location: [this.data.location.lng, this.data.location.lat].join(","),
        poitype: '商务住宅',
        extensions: 'all'
      }
    }).then(res => {
      let data = res.data.regeocode
      try {
        this.setData({
          address: data.formatted_address,
          city: data.addressComponent.city,
          // dtCode: data.addressComponent.adcode,
          // ctCode: data.addressComponent.adcode.slice(0, 4) + '00',
          pcCode: data.addressComponent.adcode.slice(0, 3) + '000'
        })
      } catch (err) {
        this.setData({
          address: data.addressComponent.province + data.addressComponent.city + data.addressComponent.district + data.addressComponent.street,
          city: data.addressComponent.city
        })
      }
      wx.setStorage({
        key: 'address',
        data: this.data.address,
      })
      wx.setStorage({
        key: 'pcCode',
        data: this.data.pcCode,
      })
    })
  },
  //打开定位地址
  openLocation() {
    wx.openLocation({
      latitude: this.data.location.lat,
      longitude: this.data.location.lng,
      address: this.data.address
    })
  },
  openSetting() {
    this.handGetLocation().then(this.loadAddress)
  },
  onPullDownRefresh() {
    this.loadData()
  }
})